unit ABM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, DB, Grids, DBGrids, JvExDBGrids, JvDBGrid,
  JvDBUltimGrid, JvExControls, JvSpeedButton, StdCtrls, IniFiles,
  JvComponentBase, JvFormPlacement;

type
  TF_ABM = class(TForm)
    PanelInferior: TPanel;
    Panel_Superior: TPanel;
    PanelCentral: TPanel;
    PanelOpciones: TPanel;
    PanelGrilla: TPanel;
    JvDBUltimGrid: TJvDBUltimGrid;
    B_AgregarMaterias: TJvSpeedButton;
    B_Guardar: TJvSpeedButton;
    B_Modificar: TJvSpeedButton;
    B_Eliminar: TJvSpeedButton;
    B_Cerrar: TJvSpeedButton;
    L_Buscar: TLabel;
    DataSet: TDataSource;
    Edit_Buscar: TEdit;
    JvSB_Filtrar: TJvSpeedButton;
    GB_OpcionesDeFiltrado: TGroupBox;
    JvFormStorage: TJvFormStorage;
    procedure B_GuardarClick(Sender: TObject);
    procedure B_CerrarClick(Sender: TObject);
    procedure NumericEditKeyPress(Sender: TObject;  var Key: Char);
    procedure JvDBUltimGridDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }

    procedure LoadWindowConfiguration;
    procedure SaveWindowConfiguration;
  public
    { Public declarations }
    TipoCuenta: integer;
    constructor Create (var owner: TComponent; AccountType:integer);
    // Activar Botones
    procedure EnableLimitedUser();
    procedure EnableAdministrator();
  end;

var
  F_ABM: TF_ABM;

implementation

uses DM_Persistencia, F_Funciones;

{$R *.dfm}

constructor TF_ABM.Create (var owner: TComponent; AccountType:integer);
begin
  inherited Create(owner);
  TipoCuenta:= AccountType;
  case AccountType of
    1: EnableLimitedUser();
    2: EnableAdministrator();
  end;
end;

procedure TF_ABM.B_GuardarClick(Sender: TObject);
begin
  Close;
end;

procedure TF_ABM.B_CerrarClick(Sender: TObject);
begin
  Close;
end;

procedure TF_ABM.NumericEditKeyPress(Sender: TObject;  var Key: Char);
var
  i:integer;
begin
  if not ((Key in ['0'..'9', #0..#31]) or ((Key in ['/',',']) and ((Pos('/', TEdit(Sender).Text) = 0) and (Pos(',', TEdit(Sender).Text) = 0)) and (Length(TEdit(Sender).Text)>0))) then
    begin
      Key := #0;
      MessageBeep(MB_ICONEXCLAMATION);
    end
end;

procedure TF_ABM.EnableLimitedUser();
begin
  B_AgregarMaterias.Enabled:=True;
  B_Modificar.Enabled:=False;
  B_Eliminar.Enabled:=False;
end;


procedure TF_ABM.EnableAdministrator();
begin
  B_AgregarMaterias.Enabled:=True;
  B_Modificar.Enabled:=True;
  B_Eliminar.Enabled:=True;
end;

procedure TF_ABM.JvDBUltimGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   if NOT JvDBUltimGrid.Focused then
   begin
     if (gdSelected in State) then
     begin
       with JvDBUltimGrid.Canvas do
       begin
         Brush.Color := clHighlight;
         Font.Style := Font.Style;
         Font.Color := clHighlightText;
       end;
     end;
   end;
   JvDBUltimGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;

procedure TF_ABM.SaveWindowConfiguration;
var
  MiFichero : TIniFile;
  ruta:string;
  i:integer;
begin
  ruta:= ExtractRutaConfiguracion;
  try
    MiFichero:=TIniFile.Create(ruta);

    with JvDBUltimGrid do
      for i := 0 to (Columns.Count - 1) do
        MiFIchero.WriteInteger(Caption,Columns[i].FieldName, Columns[i].Width);

    with GB_OpcionesDeFiltrado do
      begin
        for i:=0 to ControlCount-1 do
          MiFIchero.WriteBool(Caption,TCheckBox(Controls[i]).Caption, TCheckBox(Controls[i]).Checked);
      end;

    MiFIchero.WriteInteger(Caption,'Height',Height);
    MiFIchero.WriteInteger(Caption,'Width',Width);
    MiFIchero.WriteBool(Caption, 'InitMax', WindowState = wsMaximized );

  finally
    MiFIchero.Free;
  end;
end;

procedure TF_ABM.LoadWindowConfiguration;
var
  MiFichero : TIniFile;
  ruta:string;
  i:integer;
begin
  ruta:= ExtractRutaConfiguracion;
  try
    MiFichero:=TIniFile.Create(ruta);
    with JvDBUltimGrid do
      for i := 0 to (Columns.Count - 1) do
        Columns[i].Width:= MiFIchero.ReadInteger(Caption,Columns[i].FieldName,35);

    with GB_OpcionesDeFiltrado do
      begin
        for i:=0 to ControlCount-1 do
          TCheckBox(Controls[i]).Checked:= MiFIchero.ReadBool(Caption,TCheckBox(Controls[i]).Caption, false);
      end;

    Height:= MiFIchero.ReadInteger(Caption,'Height',0);
    Width:= MiFIchero.ReadInteger(Caption,'Width',0);

    if MiFIchero.ReadBool( Caption, 'InitMax', false ) then
      WindowState := wsMaximized
    else
      WindowState := wsNormal;
  finally
    MiFIchero.Free;
  end;
end;

procedure TF_ABM.FormCreate(Sender: TObject);
begin
  LoadWindowConfiguration;
end;

procedure TF_ABM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveWindowConfiguration;
end;

end.
